package javanio.buffer;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Created by IntelliJ IDEA.
 * User: liuliwei
 * Date: 2017-06-02
 * Time: 下午3:40
 * Description: ${description}
 */
public class FileChannelTest {
    public static void main(String[] args) throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile("./tttt","rw");
        FileChannel inChannel = randomAccessFile.getChannel();


        ByteBuffer buf = ByteBuffer.allocate(48);

        int bytesRead = inChannel.read(buf);

        while (bytesRead !=-1){
            System.out.println("Read" + bytesRead);
            buf.flip();

            while (buf.hasRemaining()) {
                System.out.println((char)buf.get());
            }

            buf.clear();
            bytesRead = inChannel.read(buf);
        }

        randomAccessFile.close();
    }
}
